package com.boarsoft.dao.sharding.group;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.dao.sharding.util.MappingUtil;

public class SimpleMapShardGroup extends MapShardGroup {
	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Override
	public String match(String key) {
		if (map.containsKey(key)) {
			return map.get(key);
		}
		String k = MappingUtil.simpleMatch(map.keySet(), key);
		k = (k == null ? _default : map.get(k));
		this.put(key, k);
		return k;
	}

	public void put(String k, DataSource ds) {
		log.info("Mapping key {} to datasource {}", k, ds);
		this.dataSourceMap.put(k, ds);
	}
}
